(function() {
    var Dialog = require('common:widget/ui/dialog/dialog.js').Dialog,
        dialog = new Dialog();

	var pageUtil =  {
		id_card : '',
        pi:1,
        trade_passwd_status : '',
		init: function() {
            var util = require('common:widget/ui/libs/util.js').util,
                cardslider = require('home:widget/ui/cardslider/cardslider.js'),
			    me = this,urlParam = util.getUrlParas();
			me.usable_amount = 0;
            if(urlParam['pid']){
                me.pid = urlParam['pid'];
            }
			me.cardslider = new cardslider($('.bank_card_block .container'));
			this.eventCenter();
			this.getUserFund();
			this.getValidateInfo();
		},
		eventCenter : function() {
			var me = this;
			$('.input_text').click(function(event) {
				$(event.target).find('input').focus();
			})
			$('.form_el input').change(function() {
				me.checkForm(event.target);
			});
			$('.vc_button').bind('click', {'me':me},me.sendVC);
			$("#amount").keypress(function(event) {
				var keyCode = event.which;
				if (keyCode == 46 || (keyCode >= 48 && keyCode <=57))
					return true;
				else
					return false;
			}).focus(function() {
				this.style.imeMode='disabled';
			});
			$('#withdraw_button').click(function() {
				var input_list = $('.form_el input');
				for(var i=0; i < input_list.length; i++) {
					var one_el = input_list[i];
					var status = me.checkForm(one_el);
					if(!status) {
						return;
					}
				}
				me.submitForm();
			})
		},
		getUserFund: function() {
			var me = this;
			$.getJSON('/user/userfundsummary',{pid:me.pid},function(data) {
				if(data && data.error_no==0 && data.data) {
					$('#total_amount .ob_title').text('￥' + mathUtil.formatAmount(data.data.total,2));

                    if(me.pid==2 || me.pid==3){
                        me.usable_amount = parseFloat(data.data.waiting_amount) +
                            parseFloat(data.data.amount) +
                            parseFloat(data.data.profit);
                    }else{
                        me.usable_amount = parseFloat(data.data.total) - parseFloat(data.data.freeze);
                    }

					$('#usable_amount .ob_title').text('￥' + mathUtil.formatAmount(me.usable_amount,2));
				} else {
					if(data.error_no != 0) {
						dialog.show(data.error_message);
					}
				}
			});
		},
		getValidateInfo : function() {
			var me = this;
			$.getJSON('/account/getuserstatus', function(data) {
				if(data && data.error_no == 0) {
					me.id_card = data.data.id_card;
					me.trade_passwd_status = data.data.trade_passwd_status;
				}
			});
		},
		checkForm: function(html) {
			var me = this;
			var id = $(html).attr('id');
			var jId = '#' + id;
			me.clearMessage($(jId));
			if(me.cardslider.selectedBankCardId == 0) {
				me.setAlertify('请选择需要提现的银行卡。');
				return false;
			}
			if(id == 'amount') {
				if(!$(jId).val() || $(jId).val() == '0'){
					me.setMessage($(jId), '请输入需要提现的金额');
					return false;
				} else if(!/^[0-9]+(.[0-9]{1,2})?$/.test($(jId).val())) {
					me.setMessage($(jId), '金额最多到小数点两位');
					return false;
				} else if(me.usable_amount < parseFloat($(jId).val())) {
					me.setMessage($(jId), '金额不可超过可提现金额');
					return false;
				}else if(parseFloat($(jId).val()) <= 2) {
					me.setMessage($(jId), '您本次提现小于2元，而每次提现第三方收取2元手续费。');
					return false;
				}else if(parseFloat($(jId).val()) > 50000){
                    me.setMessage($(jId), '单次提现的上限为5万');
                    return false;
                }
			} else if(id == 'vc') {
				if($(jId).val().length < 6) {
					me.setMessage($(jId), '请输入6位验证码。');
					return false;
				}
			} else if(id == 'trade_passwd') {
				if($(jId).val().length < 6 || $(jId).val().length > 16) {
					me.setMessage($(jId),'密码长度需要6~16位。');
					return false;
				}
			}
			if(!me.id_card) {
				if(!me.idverify) {
					var IDVerify = require('home:widget/ui/idverify/idverify.js');
					me.idverify = new IDVerify();
					$('#idverify_submit').bind('click',function() {
				        var opt = me.idverify.checkForm();
				        if(!opt) {
				        	return;
				        }
				        $.post('/account/setidcard',opt,function(data) {
				            if(data && data.error_no == 0) {
				            	dialog.show('实名认证成功');
				            	me.id_card = opt.id_card;
				            	me.idverify.hide();
				            } else {
				            	me.idverify.setError(data.error_message);
				            }
				        },'json');
				    });
				}
				me.idverify.show();
				return;
			}
			return true;
		},
		setAlertify: function(error_message) {
			var me = this;
			if(me.alertify) {
				me.alertify.setMessage(error_message);
			} else {
				var Alertify = require('home:widget/ui/alertify/alertify.js');
				me.alertify = new Alertify();
				me.alertify.setMessage(error_message);
			}
		},
		clearMessage:function($container) {
			$container.parent().parent().find('.info').text('');
		},
		setMessage:function($container,message,type) {
			$('.form_el .info').text('');
			type = type || 'error_msg';
			$infoContainer = $container.parent().parent().find('.info');
			if(message.length > 15) {
				$infoContainer.css('margin-top','0px');
			} else{
				$infoContainer.css('margin-top','20px');
			}
			$infoContainer.html(message);
			$infoContainer.removeClass('error_msg');
			$infoContainer.removeClass('info_msg');
			$infoContainer.addClass(type);
		},
		submitForm: function() {
			var me = this;
			var amount = $('#amount').val();
			var vc = $('#vc').val();
			var password = $('#trade_passwd').val();
			$.post('/pay/createwithdrawserial',{
                'bankcard_id':me.cardslider.selectedBankCardId,
                'amount':amount,
                'pid':me.pid,
                'code':vc,
                'trade_passwd': password
            } ,function(data) {
				if(data && data.error_no == 0) {
					location.href='/user/myrecord';
				} else {
					if(data.error_no == 10101 || data.error_no == 10102) {
						me.setMessage($('#amount'),data.error_message);
					} else if(data.error_no == 10203 || data.error_no == 10204) {
						me.setMessage($('#vc'),data.error_message);
					} else {
						dialog.show(data.error_message);
					}

				}
			},'json');
		},
		sendVC : function(event) {
			var type = 0;
			if(event.data && event.data.type) {
				type = event.data.type;
			}
			$('.get_vc').text(60);
			$('.get_vc').unbind('click');
			$.post('/account/sendmyidentitycode',{'type':type},function(data) {
				event.data.me.clearMessage($('#vc'));
				if(data && data.error_no == 0) {
					var setTime = setInterval(function(){
						var time = $('.get_vc').text();
							if(time == 1) {
								$('.get_vc').text('获取');
								$('.get_vc').removeClass('disabled');
								$('.get_vc').bind('click',{'me':event.data.me},event.data.me.sendVC);
								clearInterval(setTime);
								event.data.me.setMessage($('#vc'), '如果1分钟内没有收到校验短信，您可以通过<a id="get_voice_vc">语音方式</a>获取验证码，此服务免费。','info_msg');
								$('#get_voice_vc').css('cursor','pointer').css('color','#FFF').css('text-decoration','underline').css('padding','0px 10px');
								$('#get_voice_vc').bind('click',{'me':event.data.me, 'type':1},event.data.me.sendVC);
							} else {
								$('.get_vc').text(parseInt(time) -1);
							}
					},1000);
				} else {
					event.data.me.setMessage($('#vc'),data.error_message);
				}
			},'json');
		}
	}
	pageUtil.init();
})();